package com.bsoft.rpc.basic.codec;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

/**
 * 客户端消息处理类
 */
@Slf4j
public class NettyClientHandler extends SimpleChannelInboundHandler<MsgProtocol> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MsgProtocol massage) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i++) {
            String msg = "hi,server";
            byte[] content = msg.getBytes(Charset.forName("utf-8"));

            MsgProtocol protocol = new MsgProtocol();
            protocol.setContent(content);
            protocol.setLength(content.length);

            ctx.channel().writeAndFlush(protocol);
        }
    }
}
